﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BreukTrainer_EE_.Controller;

namespace BreukTrainer_EE_.View
{
    public partial class ViewResultaat : Form
    {
        public ViewResultaat()
        {
            InitializeComponent();
        }
        private long opdrachtid;
        private long leerlingid;

        private void opdrachtBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.opdrachtBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.breukTrainerDBDataSet);

        }

        private void ViewResultaat_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'breukTrainerDBDataSet.Opdracht' table. You can move, or remove it, as needed.
            this.opdrachtTableAdapter.Fill(this.breukTrainerDBDataSet.Opdracht);
        }

        private void opdrachtDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView data = (DataGridView)sender;
            int index = e.RowIndex;
            if (index == -1)
            {
                //doe niets, dit is om exceptions te voorkomen
                return;
            }
            else
            {
                opdrachtid = (long)data.Rows[index].Cells[0].Value;
                leerlingTableAdapter.FillByOpdrachtID(this.breukTrainerDBDataSet.Leerling, opdrachtid);
                for (int i = 0; i < dgvLeerlingen.Rows.Count; i++) {
                    bool afgewerkt = Accessor.isAfgewerkt((long)dgvLeerlingen.Rows[i].Cells[0].Value,opdrachtid);
                    dgvLeerlingen.Rows[i].Cells[3].Value = afgewerkt;
                    if (afgewerkt) {
                        dgvLeerlingen.Rows[i].Cells[4].Value = (double)Accessor.getScore((long)dgvLeerlingen.Rows[i].Cells[0].Value, opdrachtid);
                        dgvLeerlingen.Rows[i].Cells[5].Value = Accessor.getMaxScore(opdrachtid);
                    }
                }
            }
        }

        private void dgvLeerlingen_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView data = (DataGridView)sender;
            int index = e.RowIndex;
            if (index == -1)
            {
                //doe niets, dit is om exceptions te voorkomen
                return;
            }
            else
            {
                leerlingid = (long)data.Rows[index].Cells[0].Value;
                leerlingViewTableAdapter.FillOpdractDetailsLeerling(this.breukTrainerDBDataSet.LeerlingView, leerlingid, opdrachtid);
                for (int i = 0; i < dgvLeerlingOpdDet.Rows.Count;i++)
                {
                    if ((bool)dgvLeerlingOpdDet.Rows[i].Cells[4].Value)
                    {
                        dgvLeerlingOpdDet.Rows[i].Cells[3].Style.BackColor = Color.Red;
                    }
                    else {
                        dgvLeerlingOpdDet.Rows[i].Cells[3].Style.BackColor = Color.Green;
                    }
                }
            }
        }

        private void ViewResultaat_FormClosed(object sender, FormClosedEventArgs e)
        {
            new ViewOpdrachten().Show();
        }
    }
}
